
//页面元数据定义文件名，这里是debug使用，可以直接传给对应函数
var pageMetaDef = "";

/**
** usage：转换原始页面数据模型到页面结构模型，便于设置运营参数；
**/

//transform page mode to actual page schema obj:
function transToPageModeMeta(rawMetaObj){
	var pageMeta2={};
	pageMeta2["type"] ="object"; 
	for(var k in rawMetaObj){
		if(k === "pageBlocks"){
			//for(var kk in rawMetaObj[k]){
			//	if(kk === "properties"){
				if(!pageMeta2["properties"]) pageMeta2["properties"] = {}; 
				pageMeta2["properties"]['pageBlocks'] = {};

				pageMeta2["properties"]['pageBlocks']['title'] = "页面区域定义";
				pageMeta2["properties"]['pageBlocks']['type'] = "object";

				pageMeta2["properties"]['pageBlocks']["properties"] =  buildPageBlocks(rawMetaObj[k]);

			//	}
			//	else
			//		pageMeta2[kk] = rawMetaObj[k]['title'];
			//}
		}
		else{
			if(k === "pageVars"){
				pageMeta2["properties"]["pageVars"] = buildArray(rawMetaObj[k]);
			}
			else{
				pageMeta2[k] = rawMetaObj[k];
			}
		}
	}
	
	console.log("page meta info:\n" + JSON.stringify(pageMeta2));
	return JSON.stringify(pageMeta2);
	//return false;
}


function buildPageBlocks(rawPageBlocks){
	var ret = {};
	for(var b in rawPageBlocks){
		ret[b] = buildObject(rawPageBlocks[b]);
	}
	return ret;
}

function buildObject(blockDef){ 
	var ret = {};
	for(var k in blockDef){
		var to = blockDef[k];
		if(k === "properties" ){
			if(to ){
				ret[k] = {};
				for(var pk in to){	 //for each prop
					var prop = to[pk];
					if(prop["type"]){
						var t = prop["type"];
						if(t === "object"){
							ret[k][pk] = buildObject(prop);
						}
						else
							if(t === "array"){
								ret[k][pk] = buildArray(prop);
							}
							else if(t === "TArrayDef"){								
								var tobj = buildObject(prop);
								tobj["type"] = "object";
								tobj["$ref"] = "/widget/"+t+".json";
								ret[k][pk] = tobj;
								//ret[k][pk] = buildBuildInArrayType(prop);	
							}
							else{
								//prop is buildin type:
								var bp = {};
								for(var tpk in prop){
									bp[tpk] = prop[tpk];
								}
								bp["type"] = "object";
								bp["$ref"] = "/widget/"+t+".json";
								ret[k][pk] = bp;
							}
					}
				}
			}
			else
				ret[k] = b[k];
		}
		else{
			ret[k] = to;
		}
	}
	if(blockDef["title"]) ret["title"] = blockDef["title"];
	ret["type"] = "object";

	return ret;
}

function buildArray(prop){
	var ret = {};
	for(var i in prop){
		if(i === "items"){
			ret[i] = buildObject(prop[i]);
		}
		else{
			ret[i] = prop[i];
		}
	} 
	ret['type'] = "array";
	return ret;
}

function buildBuildInArrayType(prop){

}